package servlet;

import pojo.News;
import service.NewsService;
import service.NewsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class GetNewsById extends HttpServlet {
    NewsService newsService = new NewsServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");
        String newsId = req.getParameter("id");
        News news = newsService.getNewsById(Integer.parseInt(newsId));
        Integer views = news.getViews() + 1;
        news.setViews(views);
        newsService.setViewsById(news);
        req.setAttribute("news",news);
        req.getRequestDispatcher("news.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
